﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVT.DAO;
using QLVT.Entity;

namespace QLVT.UI.UserControlUI
{
    public partial class UcAccount : UserControl
    {
        bool add = false;
        public UcAccount()
        {
            InitializeComponent();
        }

        private void UcAccount_Load(object sender, EventArgs e)
        {
            TaiKhoanDAO taiKhoanDAO = new TaiKhoanDAO();
            DataSet ds = taiKhoanDAO.GetAll();
            DataGridViewTextBoxColumn columnUsername = new DataGridViewTextBoxColumn();
            columnUsername.HeaderText = "Tên tài khoản";
            columnUsername.DataPropertyName = "username";
            dataGridViewTaiKhoan.Columns.Add(columnUsername);
            DataGridViewTextBoxColumn columnQuyen = new DataGridViewTextBoxColumn();
            columnQuyen.HeaderText = "Quyền";
            columnQuyen.DataPropertyName = "quyen";
            dataGridViewTaiKhoan.Columns.Add(columnQuyen);
            dataGridViewTaiKhoan.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewTaiKhoan.DataSource = ds.Tables[0];
        }

        private void dataGridViewTaiKhoan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow rowSelect = dataGridViewTaiKhoan.SelectedRows[0];
            TaiKhoanDAO taiKhoanDao = new TaiKhoanDAO();
            TaiKhoan nhanvien = taiKhoanDao.GetById(rowSelect.Cells[0].Value.ToString());
            if (nhanvien != null)
            {
                BinddingData(nhanvien);
            }
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
            }
        }

        public void BinddingData(TaiKhoan taiKhoan)
        {
            txtUsername.Text = taiKhoan.Username;
            txtConfirmPassword.Text = taiKhoan.Password;
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            String manv = txtUsername.Text;
            if (manv.Length != 0)
            {
                TaiKhoanDAO taiKhoanDAO = new TaiKhoanDAO();
                if (!taiKhoanDAO.DeleteById(manv))
                {
                    MessageBox.Show("Không xóa được nhân viên", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = taiKhoanDAO.GetAll();
                    dataGridViewTaiKhoan.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(taiKhoanDAO.GetById(dataGridViewTaiKhoan.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        public TaiKhoan getData()
        {
            TaiKhoan taiKhoan = null;
            try
            {
                taiKhoan = new TaiKhoan();
                taiKhoan.Username = txtUsername.Text;
                taiKhoan.Password = txtConfirmPassword.Text;
                return taiKhoan;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            TaiKhoan taiKhoan = getData();
            if (taiKhoan != null)
            {
                TaiKhoanDAO taiKhoanDAO = new TaiKhoanDAO();
                if (!taiKhoanDAO.UpdateById(taiKhoan))
                {
                    MessageBox.Show("Không cập nhật được nhân viên", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = taiKhoanDAO.GetAll();
                    dataGridViewTaiKhoan.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(taiKhoanDAO.GetById(dataGridViewTaiKhoan.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            if (!add)
            {
                toolStripButtonOk.Visible = true;
                add = true;
                toolStripButtonAdd.Visible = false;
                txtUsername.Text = null;
                
                txtConfirmPassword.Text = null;
            }
        }

        private void toolStripButtonOk_Click(object sender, EventArgs e)
        {
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
                TaiKhoan taiKhoan = getData();
                if (taiKhoan != null)
                {
                    TaiKhoanDAO taiKhoanDAO = new TaiKhoanDAO();
                    if (!taiKhoanDAO.Insert(taiKhoan))
                    {
                        MessageBox.Show("Không tạo thêm được nhân viên", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DataSet ds = taiKhoanDAO.GetAll();
                        dataGridViewTaiKhoan.DataSource = ds.Tables[0];
                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            BinddingData(taiKhoanDAO.GetById(dataGridViewTaiKhoan.Rows[0].Cells[0].Value.ToString()));
                        }
                    }
                }
            }
        }
    }
}
